package com.ltu.schedule;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ltu.domain.mp_entity.PayRecord;
import com.ltu.domain.mp_entity.customize.CompanyEntity;
import com.ltu.rest.wxlive.WxMaLiveRestService;
import com.ltu.service.PayRecordService;

import lombok.extern.slf4j.Slf4j;



@Slf4j
@Component
@Order(99)
public class ActivitySchedule { 
	@Autowired
	private WxMaLiveRestService wxMaLiveRestService;
	@Autowired
	private PayRecordService payRecordService;
	
	@Autowired
	private CompanyEntity wxMaMiniAppCfg;
	
/**
 * 数据统计定时器
 *  CRON规则示例：
 * 	0 0 3 * * ?     每天3点执行
 * 	0 5 3 * * ?     每天3点5分执行
 * 	0 5 3 ? * *     每天3点5分执行，与上面作用相同
 * 	0 5/10 3 * * ?  每天3点的 5分，15分，25分，35分，45分，55分这几个时间点执行
 *  0 10 3 ? * 1    每周星期天，3```````````````````````个月的第三个星期，星期天 执行，#号只能出现在星期的位置
 */


	/**
	 * 发货
	 */
	@Scheduled(cron = "0 0 12 * * ?")
	public void  updateSendOrder() {
		
		QueryWrapper<PayRecord>  qw = new QueryWrapper<PayRecord> ();
		qw.lambda().eq(PayRecord :: getStatus, 1)
					.eq(PayRecord :: getSendState, 0)
					;
		List<PayRecord>  ps = this.payRecordService.list(qw);	
		for (PayRecord p : ps) {
			try {
				wxMaLiveRestService.sendOrder(p, wxMaMiniAppCfg);
				p.setSendState(1);
			} catch (Exception e) {
				// TODO: handle exception
				log.error(e.getMessage());
			}
		
		}
		this.payRecordService.updateBatchById(ps);
	}

}
